﻿using System.Linq;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

[assembly: CommandClass(typeof(DonetArxLib.DoubleClickHandler.DoubleClickCommands))]

namespace DonetArxLib.DoubleClickHandler
{
    public class DoubleClickCommands
    {
        #region commands

        [CommandMethod("MyLineEditorCommand1", CommandFlags.UsePickSet)]
        public void RunMyLineEditorCommand1()
        {
            ObjectId entId = GetSelectedEntity();
            if (entId == ObjectId.Null)
                return;

            string msg = $"你双击了线段编辑器命令1！此线从左向右 \n EntityId: {entId.ToString()}";
            Application.ShowAlertDialog(msg);
        }

        [CommandMethod("MyLineEditorCommand2", CommandFlags.UsePickSet)]
        public void RunMyLineEditorCommand2()
        {
            ObjectId entId = GetSelectedEntity();
            if (entId == ObjectId.Null)
                return;
            string msg = $"你双击了线段编辑器命令2！此线从右向左 \n EntityId: {entId.ToString()}";
            Application.ShowAlertDialog(msg);
        }

        [CommandMethod("MyBlockEditorCommand1", CommandFlags.UsePickSet)]
        public void RunMyBlockEditorCommand1()
        {
            ObjectId entId = GetSelectedEntity();
            if (entId == ObjectId.Null)
                return;
            string msg = $"你双击了块编辑器命令1！块为<testblock1> \n EntityId: {entId.ToString()}";
            Application.ShowAlertDialog(msg);
        }

        [CommandMethod("MyBlockEditorCommand2", CommandFlags.UsePickSet)]
        public void RunMyBlockEditorCommand2()
        {
            ObjectId entId = GetSelectedEntity();
            if (entId == ObjectId.Null)
                return;
            string msg = $"你双击了块编辑器命令2！块为<testblock2> \n EntityId: {entId.ToString()}";
            Application.ShowAlertDialog(msg);
        }

        [CommandMethod("MyTextEditorCommand", CommandFlags.UsePickSet)]
        public void RunMyTextEditorCommand()
        {
            ObjectId entId = GetSelectedEntity();
            if (entId == ObjectId.Null)
                return;

            string content = "";
            using (var trans = entId.Database.TransactionManager.StartOpenCloseTransaction())
            {
                DBText text = UtilFunc.GetObject(trans, entId, OpenMode.ForRead) as DBText;
                content = text.TextString;
                trans.Commit();
            }


            string msg = $"你双击了文本编辑器命令！文本为<{content}> \n EntityId: {entId.ToString()}";
            Application.ShowAlertDialog(msg);
        }

        #endregion

        private ObjectId GetSelectedEntity()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            var res = ed.GetSelection();

            if (res.Status == PromptStatus.OK)
            {
                return res.Value.GetObjectIds().FirstOrDefault();
            }

            return ObjectId.Null;
        }
    }
}
